package com.xsd.awen.shiro;

import com.xsd.awen.domain.entity.User;
import com.xsd.awen.domain.po.LoginPo;
import com.xsd.awen.service.UserService;
import com.xsd.awen.utils.GsonUtils;
import com.xsd.awen.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("authorizer")
public class CustomRealm extends AuthorizingRealm {

    Logger logger = LoggerFactory.getLogger(AuthorizingRealm.class);

    @Autowired
    private UserService userService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("*******************111*****************");
        //获取登录用户名
        String name = (String) principalCollection.getPrimaryPrincipal();
        //查询用户名称
        //User user = null;//cService.selectByUsername(name);

        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        //下述两行可无限制重复添加
        //添加角色
        simpleAuthorizationInfo.addRole("lcdp");
        //添加权限
        simpleAuthorizationInfo.addStringPermission("select");
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        logger.info("login auth");
        //获取用户信息
        Object usernameObj = authenticationToken.getPrincipal();
        if( usernameObj == null ){
            throw new RuntimeException("username is null");
        }
        Object credentials = authenticationToken.getCredentials();
        if( credentials == null ){
            throw new RuntimeException("credentials is null");
        }
        String password = new String((char[])credentials);
        String usernmae = usernameObj.toString();

        User queryUser = new User();
        queryUser.setCode(usernmae);
        User user = userService.getUserByUsername(queryUser);

        if (user == null || !user.getPwd().equals(password)) {
            System.out.println("user pass wrong");
            System.out.println("用户名密码错误");
            System.out.println(GsonUtils.toJson(user));
            System.out.println(password);
            System.out.println(user.getPwd());
            //这里返回后会报出对应异常
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息

            LoginPo po = new LoginPo();
            po.setId(UUIDUtils.generate());
            po.setName(user.getName());
            po.setUsername(user.getCode());
            po.setUserId(user.getId());
            po.setTime(System.currentTimeMillis());
            po.setTk(SecurityUtils.getSubject().getSession().getId().toString());
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(po, user.getPwd(), getName());
            return simpleAuthenticationInfo;
        }
    }
}